﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace LCL.Config
{
    /// <summary>
    /// AppSeting配置信息
    /// </summary>
    public class AppSetingInfo : IConfigInfo
    {
        public AppSetingInfo()
        {
            NameSpace = "Agile";
            TableFormat = "uc_|lcl_|table_|zt_|lmg_|zte_|cms_|tky_|base_|sys_|pre_common_|pre_flow_|pre_personnel_|pre_rbac_|school_|rbac_|flow_|system_|personnel_|school_|salary_|_";
        }
        public string NameSpace { get; set; }
        public string TableFormat { get; set; }
    }

    /// <summary>
    ///  AppSeting配置类
    /// </summary>
    public partial class AppSeting
    {
        /// <summary>
        /// 获取配置类实例
        /// </summary>
        /// <returns></returns>
        public static AppSetingInfo GetConfig()
        {
            return AppSetingFileManager.LoadConfig();
        }
        /// <summary>
        /// 保存配置类实例
        /// </summary>
        /// <param name="configinfo"></param>
        /// <returns></returns>
        public static bool SaveConfig(AppSetingInfo configinfo)
        {
            AppSetingFileManager rcfm = new AppSetingFileManager();
            AppSetingFileManager.ConfigInfo = configinfo;
            return rcfm.SaveConfig();
        }
    }

    /// <summary>
    /// AppSeting配置管理类
    /// </summary>
    public partial class AppSetingFileManager : DefaultConfigFileManager
    {
        private static AppSetingInfo m_configinfo;
        /// <summary>
        /// 文件修改时间
        /// </summary>
        private static DateTime m_fileoldchange;
        /// <summary>
        /// 初始化文件修改时间和对象实例
        /// </summary>
        static AppSetingFileManager()
        {
            Path.GetPathRoot(ConfigFilePath).CreateDir();
            if (ConfigFilePath.FileExists())
            {
                m_fileoldchange = System.IO.File.GetLastWriteTime(ConfigFilePath);
                m_configinfo = (AppSetingInfo)DefaultConfigFileManager.DeserializeInfo(ConfigFilePath, typeof(AppSetingInfo));
            }
        }
        /// <summary>
        /// 当前的配置类实例
        /// </summary>
        public new static IConfigInfo ConfigInfo
        {
            get { return m_configinfo; }
            set { m_configinfo = (AppSetingInfo)value; }
        }
        /// <summary>
        /// 配置文件所在路径
        /// </summary>
        public static string filename = null;
        /// <summary>
        /// 获取配置文件所在路径
        /// </summary>
        public new static string ConfigFilePath
        {
            get
            {
                if (filename == null)
                {
                    filename = Utils.GetMapPath("~/config/AppSeting.config");
                }
                return filename;
            }
        }
        /// <summary>
        /// 返回配置类实例
        /// </summary>
        /// <returns></returns>
        public static AppSetingInfo LoadConfig()
        {
            if (ConfigFilePath.FileExists())
            {
                ConfigInfo = DefaultConfigFileManager.LoadConfig(ref m_fileoldchange, ConfigFilePath, ConfigInfo);
                return ConfigInfo as AppSetingInfo;
            }
            else
            {
                Logger.Warn("db config:" + ConfigFilePath);
                return null;
            }
        }
        /// <summary>
        /// 保存配置类实例
        /// </summary>
        /// <returns></returns>
        public override bool SaveConfig()
        {
            return base.SaveConfig(ConfigFilePath, ConfigInfo);
        }
    }
}
